/*
 * Copyright 2009 Codecarpet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.codecarpet.fbconnect.sample;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.codecarpet.fbconnect.FBDialog;
import com.codecarpet.fbconnect.FBFeedActivity;
import com.codecarpet.fbconnect.FBLoginButton;
import com.codecarpet.fbconnect.FBPermissionActivity;
import com.codecarpet.fbconnect.FBRequest;
import com.codecarpet.fbconnect.FBSession;
import com.codecarpet.fbconnect.FBDialog.FBDialogDelegate;
import com.codecarpet.fbconnect.FBLoginButton.FBLoginButtonStyle;
import com.codecarpet.fbconnect.FBRequest.FBRequestDelegate;
import com.codecarpet.fbconnect.FBSession.FBSessionDelegate;

public class MainActivity extends Activity {
	private static final String LOG = "FBConnectSample";

	// /////////////////////////////////////////////////////////////////////////////////////////////////
	// This application will not work until you enter your Facebook
	// application's API key here:

	private static final String API_KEY = "112929505411860"; // "<YOUR API KEY>";

	// Enter either your API secret or a callback URL (as described in
	// documentation):
	private static final String API_SECRET = "133f2d1264f2d09e9d111e4c15551387"; // "<YOUR SECRET KEY>";
	private static final String GET_SESSION_PROXY = null; // "<YOUR SESSION CALLBACK)>";

	private static final int PERMISSION_REQUESTCODE = 1;
	private static final int MESSAGE_PUBLISHED = 2;
	// /////////////////////////////////////////////////////////////////////////////////////////////////

	private FBSession mSession;
	private FBLoginButton mLoginButton;
	private TextView mLabel;
	private Button mPermissionButton;
	private Button mFeedButton;
	private Button mFeedDirectButton;
	private Handler mHandler;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mHandler = new Handler();

		if (GET_SESSION_PROXY != null) {
			mSession = FBSession.getSessionForApplication_getSessionProxy(
					API_KEY, GET_SESSION_PROXY, new FBSessionDelegateImpl());
		} else {
			mSession = FBSession.getSessionForApplication_secret(API_KEY,
					API_SECRET, new FBSessionDelegateImpl());
		}

		setContentView(R.layout.main);

		mLabel = (TextView) findViewById(R.id.label);
		mPermissionButton = (Button) findViewById(R.id.permissionButton);
		mPermissionButton.setOnClickListener(new OnClickListener() {

			
			public void onClick(View v) {
				askPermission();
			}
		});

		mFeedButton = (Button) findViewById(R.id.feedButton);
		mFeedButton.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				publishFeed();
			}
		});

		mFeedDirectButton = (Button) findViewById(R.id.feedDirectButton);
		mFeedDirectButton.setOnClickListener(new OnClickListener() {

			
			public void onClick(View arg0) {
				publishFeed();
			}
		});

		mLoginButton = (FBLoginButton) findViewById(R.id.login);
		mLoginButton.setStyle(FBLoginButtonStyle.FBLoginButtonStyleWide);
		mLoginButton.setSession(mSession);

		mSession.resume(this);

	}

	// /////////////////////////////////////////////////////////////////////////////////////////////////

	private void askPermission() {
		Intent intent = new Intent(this, FBPermissionActivity.class);
		intent.putExtra("permissions", new String[] { "publish_stream", "read_stream" });
		this.startActivityForResult(intent, PERMISSION_REQUESTCODE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case PERMISSION_REQUESTCODE:
			if (resultCode == 1) {
				mPermissionButton.setVisibility(View.INVISIBLE);
				mFeedDirectButton.setVisibility(View.VISIBLE);
			}
		default:
			return;
		}

	}

	private void publishFeed() {
		// See http://wiki.developers.facebook.com/index.php/Stream.publish for
		// a list of all the optional parameters
//		Map<String, String> params = new HashMap<String, String>();
//		params
//				.put(
//						"attachment",
//						"{\"name\":\"Facebook Connect for Android\",\"href\":\"http://code.google.com/p/fbconnect-android/\",\"caption\":\"Caption\",\"description\":\"Description\",\"media\":[{\"type\":\"image\",\"src\":\"http://img40.yfrog.com/img40/5914/iphoneconnectbtn.jpg\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone/\"}],\"properties\":{\"another link\":{\"text\":\"Facebook home page\",\"href\":\"http://www.facebook.com\"}}}");
//
//		// publish_stream extended permission is required to call Stream.publish
//		FBRequest.request().call("Stream.publish", params);
		Intent intent = new Intent(this, StatusActivity.class);
		this.startActivity(intent);

	}

	private void publishFeedWithPrompt() {
		Intent intent = new Intent(this, FBFeedActivity.class);
		intent.putExtra("userMessagePrompt", "Example prompt");
		intent
				.putExtra(
						"attachment",
						"{\"name\":\"Facebook Connect for Android\",\"href\":\"http://code.google.com/p/fbconnect-android/\",\"caption\":\"Caption\",\"description\":\"Description\",\"media\":[{\"type\":\"image\",\"src\":\"http://img40.yfrog.com/img40/5914/iphoneconnectbtn.jpg\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone/\"}],\"properties\":{\"another link\":{\"text\":\"Facebook home page\",\"href\":\"http://www.facebook.com\"}}}");
		this.startActivityForResult(intent, MESSAGE_PUBLISHED);
	}

	// /////////////////////////////////////////////////////////////////////////////////////////////////

	private class FBDialogDelegateImpl extends FBDialogDelegate {
		@Override
		public void dialogDidFailWithError(FBDialog dialog, Throwable error) {
			mLabel.setText(error.toString());
		}
	}

	private void checkPermission() {
		String fql = "select publish_stream from permissions where uid == "
				+ String.valueOf(mSession.getUid());
		Map<String, String> params = Collections.singletonMap("query", fql);
		FBRequest.requestWithDelegate(new FBHasPermissionRD()).call(
				"facebook.fql.query", params);
	}

	private class FBSessionDelegateImpl extends FBSessionDelegate {

		@Override
		public void sessionDidLogin(FBSession session, Long uid) {
			// we check if the user already has the permissions before
			// displaying permission button
			checkPermission();

			mHandler.post(new Runnable() {
				public void run() {
					mFeedButton.setVisibility(View.VISIBLE);
				}
			});

			String fql = "select uid,name from user where uid == "
					+ session.getUid();

			Map<String, String> params = Collections.singletonMap("query", fql);
			FBRequest.requestWithDelegate(new FBRequestDelegateImpl()).call(
					"facebook.fql.query", params);
		}

		@Override
		public void sessionDidLogout(FBSession session) {
			mHandler.post(new Runnable() {
				public void run() {
					mLabel.setText("");
					mPermissionButton.setVisibility(View.INVISIBLE);
					mFeedButton.setVisibility(View.INVISIBLE);
				}
			});
		}

	}

	private class FBRequestDelegateImpl extends FBRequestDelegate {

		@Override
		public void requestDidLoad(FBRequest request, Object result) {

			String name = null;

			if (result instanceof JSONArray) {
				JSONArray jsonArray = (JSONArray) result;
				try {
					JSONObject jo = jsonArray.getJSONObject(0);
					name = jo.getString("name");
				} catch (JSONException e) {
					Log.e(LOG, "Login response error", e);
				}
			}
			mLabel.setText("Logged in as " + name);
		}

		@Override
		public void requestDidFailWithError(FBRequest request, Throwable error) {
			mLabel.setText(error.toString());
		}
	}

	private class FBHasPermissionRD extends FBRequestDelegate {

		@Override
		public void requestDidFailWithError(FBRequest request, Throwable error) {
			super.requestDidFailWithError(request, error);
		}

		@Override
		public void requestDidLoad(FBRequest request, Object result) {
			int hasPermission = 0;

			if (result instanceof JSONArray) {
				JSONArray jsonArray = (JSONArray) result;
				try {
					JSONObject jo = jsonArray.getJSONObject(0);
					hasPermission = jo.getInt("publish_stream");
					if (hasPermission == 0) {
						mHandler.post(new Runnable() {
							public void run() {
								mPermissionButton.setVisibility(View.VISIBLE);
							}
						});
					} else
						mFeedDirectButton.setVisibility(View.VISIBLE);
				} catch (JSONException e) {
					Log.e(LOG, "Permission response error", e);
				}
			}
		}
	}
}